﻿@using System.Globalization
@model  MDP.WebOpenPortal.Models.EtlSourceModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <script type="text/javascript">
        var retDBData = new Array();
        var doAction = { field: 'GUID', title: '操作', width: 10, align: 'center', formatter: function (value, rowData, rowIndex) {
            var add = true; ;
            for (var j = 0; j < retDBData.length; j++) {
                if (retDBData[j].GUID == rowData.GUID) {
                    add = false;
                }
            }
            if (add) {
                retDBData.push(rowData);
            }
            var acDB = "<select onchange='DBAction(this.options.selectedIndex," + rowIndex + ")' ><option value=''>-请选择-</option>";
            return acDB + "<option >修改</option><option >删除</option></select>";
        }
        };
        var sqlcolumns = [
                    { field: 'Host', title: '地址', width: 20 },
                    { field: 'Server', title: '数据库名', width: 20 },
                    { field: 'User', title: '用户名', width: 15 },
                    { field: 'Description', title: '描述', width: 35 },
                    doAction
            ];
        var oracolumns = [
                    { field: 'Host', title: '地址', width: 20 },
                    { field: 'Server', title: '数据库名', width: 15 },
                    { field: 'User', title: '用户名', width: 15 },
                    { field: 'Part', title: '端口', width: 10 },
                    { field: 'Description', title: '描述', width: 30 },
                    doAction
            ];
        var filecolumns = [
                    { field: 'FileUrl', title: '地址', width: 30 },
                    { field: 'FileUser', title: '用户名', width: 15 },
                    { field: 'Description', title: '描述', width: 45 },
                    doAction
            ];
        var opts = {
            height: 380,
            rows: 100,
            idField: "GUID",
            para: { TypeID: $.getUrlParam("id"), dbName: dbName },
            checkbox: true,
            columns: sqlcolumns
        };


        var connStrNum = 0;
        var systemID = '@Model.EtlSourceInfo.GUID';
        var dbName = '@Model.DbName';
        $(function () {
            if (systemID) {
                $("#txt_SourceSysName").attr("readonly", "readonly");
                $("#txt_SourceSysName").css("background-color", "#CCFFCC");
                var dbType = '@Model.EtlSourceInfo.SourceType';
                if (dbType == "1" || dbType == "2" || dbType == "4" || dbType == "8") {
                    $(".WDCList").hide();
                } else
                    $(".WDCList").show();
                initDBCol('@Model.EtlSourceInfo.SourceType');
                opts.url = "/EtlSource/GetEtlSourceDbConfigList?systemID=" + systemID + "&dbName=" + dbName;
                optsWDC.url = "/EtlSource/GetWDCConfigList?systemID=" + systemID + "&dbName=" + dbName;
                optsCB.url = "/EtlSource/GetCallBackConfigList?systemID=" + systemID + "&dbName=" + dbName;
                $.post("/EtlSource/GetEtlSourceUserList/?systemID=" + systemID + "&dbName=" + dbName, function (res) {
                    AddContactInfo(res);
                });
            }
            $("#DataGridDB").dg(opts);
            $("#DataGridWDC").dg(optsWDC);
            $("#DataGridCB").dg(optsCB);
            //--------------where-----------

            if ('@Model.EtlSourceInfo.GUID' != "") {
                $("#ddlStatus").val('@Model.EtlSourceInfo.Status');
                $("#ddlCallBackType").val('@Model.EtlSourceInfo.CallBackType');
            }
        });
        var UserData = new Array();
        function AddContactInfo(v) {
            for (var i = 0; i < v.length; i++) {
                var userid = v[i].Guid;
                if (UserData.indexOf(userid) == -1) {
                    UserData.push(userid);
                }
            }
        }
        function ChangeSourceType() {
            if ($("#ddlSourceType").val() == "1" || $("#ddlSourceType").val() == "2" || $("#ddlSourceType").val() == "4" || $("#ddlSourceType").val() == "8") {
                $(".WDCList").hide();
            } else
                $(".WDCList").show();
            retDBData = [];
            var data = { "total": 0, "rows": retDBData };
            opts["data"] = data;
            $("#DataGridDB").dg(opts);
            retWDCData = [];
            var dataWDC = { "total": 0, "rows": retWDCData };
            optsWDC["data"] = dataWDC;
            $("#DataGridWDC").dg(optsWDC);
        }
        function DBAction(i, v) {
            if (i == 1)
                showModalMany('AddDB', '/Public/AddDB?id=' + v + '&dbType=' + $("#ddlSourceType").val(), '610', '205', AddDBInfo);
            else if (i == 2)
                removeDB(v);

            $("#DataGridDB").dg(opts);
        }
        function AddDB() {
            showModalMany('AddDB', '/Public/AddDB?dbType=' + $("#ddlSourceType").val(), '610', '225', AddDBInfo);
        }
        function initDBCol(mtp) {
            var tp = $("#ddlSourceType").val();
            if (mtp)
                tp = mtp;
            switch (tp) {
                case "1":
                case "4":
                    opts.columns = sqlcolumns;
                    break;
                case "2":
                    opts.columns = oracolumns;
                    break;
                case "3":
                case "5":
                case "6":
                    opts.columns = filecolumns;
                    break;
            }
        }
        function AddDBInfo(v) {
            initDBCol();
            if (v["GUID"] == "" || v["GUID"] == -1) {
                v["GUID"] = retDBData.length;
                v["Status"] = 1;
                retDBData.push(v);
            } else
                retDBData[v["GUID"]] = v;
            var data = { "total": 0, "rows": retDBData };
            opts["data"] = data;
            $("#DataGridDB").dg(opts);
        }
        function removeDB(i) {
            if (confirm("确定要删除吗？")) {
                retDBData.removeAt(i);
                var data = { "total": 0, "rows": retDBData };
                opts["data"] = data;
                $("#DataGridDB").dg(opts);
            }
        }
        //----------Add DB end---------------- 

        //----------Add WDC begin---------------- 
        var retWDCData = new Array();
        var WildCardData = new Array();
        var optsWDC = {
            height: 380,
            rows: 100,
            idField: "GUID",
            para: {},
            checkbox: true,
            columns: [
                    { field: 'WildCard', title: '通配符', width: 30 },
                    { field: 'FileExt', title: '扩展名', width: 30 },
                    { field: 'ExtractOrder', title: '抽取顺序', width: 30, formatter: function (value, rowData, rowIndex) {
                        if (value == 1) {
                            return "时间";
                        } else
                            return "名称";
                    }
                    },
                    { field: 'GUID', title: '操作', width: 10, align: 'center',
                        formatter: function (value, rowData, rowIndex) {
                            var add = true; ;
                            for (var j = 0; j < retWDCData.length; j++) {
                                if (retWDCData[j].GUID == rowData.GUID) {
                                    add = false;
                                }
                            }
                            if (add) {
                                retWDCData.push(rowData);
                                WildCardData.push(rowData.WildCard);
                            }
                            var acWDC = "<select onchange='WDCAction(this.options.selectedIndex," + rowIndex + ")' ><option value=''>-请选择-</option>";
                            return acWDC + "<option >修改</option><option >删除</option></select>"
                        }
                    }
            ]
        };
        function WDCAction(i, v) {
            if (i == 1)
                showModalMany('AddCB', '/Public/AddWDC?id=' + v, '610', '205', AddWDCInfo);
            else
                removeWDC(v);
            $("#DataGridWDC").dg(optsWDC);
        }
        function AddWDC() {
            showModalMany('AddCB', '/Public/AddWDC', '610', '205', AddWDCInfo);
        }
        function AddWDCInfo(v) {
            if (v["GUID"] == "") {
                v["GUID"] = retWDCData.length;
                retWDCData.push(v);
            } else
                retWDCData[v["GUID"]] = v;
            var data = { "total": 0, "rows": retWDCData };
            optsWDC["data"] = data;
            $("#DataGridWDC").dg(optsWDC);
        }
        function removeWDC(i) {
            if (confirm("确定要删除吗？")) {
                retWDCData.removeAt(i);
                var data = { "total": 0, "rows": retWDCData };
                optsWDC["data"] = data;
                $("#DataGridWDC").dg(optsWDC);
            }
        }
        //----------Add WDC end---------------- 
        //----------Add CB begin---------------- 
        var retCBData = new Array();
        var optsCB = {
            height: 380,
            rows: 100,
            idField: "GUID",
            para: {},
            checkbox: true,
            columns: [
                    { field: 'CallBackType', title: '回调类型', width: 45, formatter: function (value, rowData, rowIndex) {
                        if (value == 1)
                            return "存储过程";
                        else
                            return "HTTP回调";
                    }
                    },
                    { field: 'CallBackName', title: '回调名称', width: 45 },
                    { field: 'GUID', title: '操作', width: 10, align: 'center',
                        formatter: function (value, rowData, rowIndex) {
                            var add = true; ;
                            for (var j = 0; j < retCBData.length; j++) {
                                if (retCBData[j].GUID == rowData.GUID) {
                                    add = false;
                                }
                            }
                            if (add) {
                                retCBData.push(rowData);
                            }
                            var acCB = "<select onchange='CBAction(this.options.selectedIndex," + rowIndex + ")' ><option value=''>-请选择-</option>";
                            return acCB + "<option >修改</option><option >删除</option></select>"
                        }
                    }
            ]
        };

        function CBAction(i, v) {
            if (i == 1)
                AddCB(v);
            else
                removeCB(v);
            $("#DataGridCB").dg(optsCB);
        }
        function AddCB(v) {
            showModalMany('editCB', '/Public/AddCallBack?id=' + v + '&dbType=' + $("#ddlSourceType").val(), '610', '205', AddCBInfo);
        }
        function AddCBInfo(v) {
            if (!v["GUID"]) {
                v["GUID"] = retCBData.length;
                retCBData.push(v);
            } else {
                retCBData[v["GUID"]] = v;
            }
            var data = { "total": 0, "rows": retCBData };
            optsCB["data"] = data;
            $("#DataGridCB").dg(optsCB);
        }
        function removeCB(i) {
            if (confirm("确定要删除吗？")) {
                retCBData.removeAt(i);
                var data = { "total": 0, "rows": retCBData };
                optsCB["data"] = data;
                $("#DataGridCB").dg(optsCB);
            }
        }


        function SubmitForm() {
            if ('@Model.EtlSourceInfo.Status' == '1') {
                alert("当前系统已修改，无法修改基本信息。");
                return;
            }
            var sourceType = $("#ddlSourceType").val();
            var copy = false;
            var extList = new Array();
            if (sourceType == "3")//accecc
            {
                copy = true;
                extList.push(".mdb");
                extList.push(".accdb");
            }
            else if (sourceType == "5")//excel
            {
                copy = true;
                extList.push(".xls");
                extList.push(".xlsx");
            }
            if ($('#ff').form("validate")) {

                $("#DBs").val($.toJSON(retDBData));
                $("#WDCs").val($.toJSON(retWDCData));
                $("#CBs").val($.toJSON(retCBData));
                $("#Users").val(UserData.join(","));
                $('#ff').form('submit', {
                    url: "/ETLSource/EtlSourceUpdate?dbName=" + dbName,
                    success: function (result) {
                        var arrResult = result.split(",");
                        if (arrResult[0] == "1") {
                            if (copy) {
                                for (var i = 0; i < retDBData.length; i++) {
                                    var ftp = retDBData[i]["FileUrl"].toLowerCase();
                                    if (ftp.indexOf("ftp") == 0) {
                                        $.post("/Public/CopyFTPFile", {
                                            sourceID: arrResult[1],
                                            ftpUrl: retDBData[i]["FileUrl"],
                                            fileUser: retDBData[i]["FileUser"],
                                            filePass: retDBData[i]["FilePass"],
                                            wd: WildCardData.join("$"),
                                            extList: extList.join("$")
                                        });
                                    }
                                }
                            }
                            alert("保存成功！");
                        } else if (arrResult[0] == "2") {
                            alert("系统名称已存在!");
                        } else {
                            alert(arrResult[0]);
                        }
                    }
                });
            }
        }

        function btnAddDB_Click() {
            AddDB();
        }
        function btnDeleteDB_Click() {
            DelDB();
        }

        function btnAddWDC_Click() {
            AddWDC();
        }
        function btnDeleteWDC_Click() {
            DelWDC();
        }

        function btnAddCB_Click() {
            AddCB("");
        }
        function btnDeleteCB_Click() {
            DelCB();
        }
        function btnSave_Click() {
            SubmitForm();
        }
        function btnClose_Click() {
            window.close();
        }
    </script>
    <link href="/CSS/indexstyle.css" rel="stylesheet" type="text/css" />
}
<div class="top">
    <div class="logo_box">
    </div>
    <div id="mainctrl">
        <div style="padding-right: 5px; text-align: right; color: #ffffff; font-family: 微软雅黑;
            float: right">
            <table>
                <tr>
                    <td>
                        您好：<span style="font-size: 13px;">@(Session["MDP_UN_Open"] == null ? "" : HttpUtility.UrlDecode(Session["MDP_UN_Open"].ToString()))</span><br />
                        欢迎您的光临！
                    </td>
                </tr>
            </table>
        </div>
    </div>
</div>
<div style="margin: 5px;">
    <form id="ff" method="post">
    @{
        // 渲染输出列表
        var dataToolbar = new ViewDataDictionary();
        dataToolbar.Add("PanelTitle", "添加源系统信息");
        dataToolbar.Add("GridID", "DataGrid");
        dataToolbar.Add("ShowAddButton", false);
        dataToolbar.Add("ShowDeleteButton", false);
        dataToolbar.Add("ShowSaveButton", true);
        dataToolbar.Add("ShowOtherButton", false);
        dataToolbar.Add("ShowBackButton", false);
        dataToolbar.Add("ShowCloseButton", true);
        dataToolbar.Add("ShowPager", true);
        dataToolbar.Add("ShowLoading", true);
        @Html.Partial("_ToolBarPanel", dataToolbar);
    }
    @Html.Hidden("systemID", @Model.EtlSourceInfo.GUID)
    @Html.Hidden("GUID", @Model.EtlSourceInfo.GUID)
    @Html.Hidden("dbName", @Model.DbName)
    <input type="hidden" id="DBs" name="DBs" />
    <input type="hidden" id="WDCs" name="WDCs" />
    <input type="hidden" id="CBs" name="CBs" />
    <input type="hidden" id="Users" name="Users" />
    <table class="table_bg" style="width: 100%;" cellpadding="0" cellspacing="0">
        <colgroup>
            <col style="width: 200px;" />
            <col style="" />
        </colgroup>
        <tr>
            <td style="text-align: right;">
                <span class="red">*</span>系统名称：
            </td>
            <td>
                <input type="text" id="txt_SourceSysName" name="SourceSysName" value="@Model.EtlSourceInfo.SourceSysName" style="width:480px" />
            </td>
        </tr>
        <tr>
            <td style="text-align: right;">
                <span class="red">*</span>联系人：
            </td>
            <td id="selected">
                @Html.Raw(@Model.StrContactList)
                @*<input type="text" id="txt_Source" name="SourceSysName" value="@Model.EtlSourceInfo.SourceSysName" style="width:480px" />*@
            </td>
        </tr>
        <tr>
            <td style="text-align: right;">
                数据源类型：
            </td>
            <td>
                @Html.DropDownList("SourceType", Model.EtlSourceTypes, new { style = "width: 150px;", @id = "ddlSourceType", @onchange = "ChangeSourceType()" })
            </td>
        </tr>
        <tr>
            <td style="text-align: right;">
                状态：
            </td>
            <td>
                <select id="ddlStatus" name="Status" style="width: 150px;">
                    <option value="2">禁用</option>
                    <option value="1">启用</option>
                </select>
            </td>
        </tr>
        <tr>
            <td style="text-align: right;" valign="top">
                描述：
            </td>
            <td>
                <textarea id="txt_SourceSysDescription" name="SourceSysDescription" rows='3' style="width: 480px">@Model.EtlSourceInfo.SourceSysDescription</textarea>
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td valign="top" height="130">
                @{
                    var _toolbarDB = new ViewDataDictionary();
                    _toolbarDB.Add("PanelTitle", "数据源连接配置 ");
                    _toolbarDB.Add("GridID", "DataGrid");
                    _toolbarDB.Add("ShowAddButton", true);
                    _toolbarDB.Add("AddButtonClick", "btnAddDB_Click()");
                    _toolbarDB.Add("ShowDeleteButton", true);
                    _toolbarDB.Add("DeleteButtonClick", "btnDeleteDB_Click()");
                    @Html.Partial("_ToolBarPanel", _toolbarDB);
                }
                <table id="DataGridDB" class="pagTable" cellpadding="0" cellspacing="0">
                </table>
            </td>
        </tr>
        <tr class="WDCList" style="display: none;">
            <td valign="top" height="130">
                @{
                    var _toolbarWDC = new ViewDataDictionary();
                    _toolbarWDC.Add("PanelTitle", "通配符配置 ");
                    _toolbarWDC.Add("GridID", "DataGrid");
                    _toolbarWDC.Add("ShowAddButton", true);
                    _toolbarWDC.Add("AddButtonClick", "btnAddWDC_Click()");
                    _toolbarWDC.Add("ShowDeleteButton", true);
                    _toolbarWDC.Add("DeleteButtonClick", "btnDeleteWDC_Click()");
                    @Html.Partial("_ToolBarPanel", _toolbarWDC);
                }
                <table id="DataGridWDC" class="pagTable" cellpadding="0" cellspacing="0">
                </table>
            </td>
        </tr>
        <tr>
            <td valign="top" height="130">
                @{
                    var _toolbarCB = new ViewDataDictionary();
                    _toolbarCB.Add("PanelTitle", "回调配置 ");
                    _toolbarCB.Add("GridID", "DataGrid");
                    _toolbarCB.Add("ShowAddButton", true);
                    _toolbarCB.Add("AddButtonClick", "btnAddCB_Click()");
                    _toolbarCB.Add("ShowDeleteButton", true);
                    _toolbarCB.Add("DeleteButtonClick", "btnDeleteCB_Click()");
                    @Html.Partial("_ToolBarPanel", _toolbarCB);
                }
                <table id="DataGridCB" class="pagTable" cellpadding="0" cellspacing="0">
                </table>
            </td>
        </tr>
    </table>
    </form>
</div>
